//
//  DMMenuFood.m
//  OrderOnline
//
//  Created by YAPPA_3 on 11/30/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DMMenuFood.h"


@interface DMMenuFood(private)
- (NSString*)getFileName: (NSString*) name;
- (NSString*)getDevicePath: (NSString*) name;
@end

@implementation DMMenuFood
- (NSString*)getFileName: (NSString*) name
{
	return [NSString stringWithFormat:@"%@%@.xml", ROOT_URL_MENU_FOOD_XML, name];
}

- (NSString*)getDevicePath:(NSString*) name
{
	return [NSString stringWithFormat:@"%%@.xml", name];
}


- (BOOL)isExist: (NSString*) name
{
	BOOL ret = NO;
	if( name )
	{
		ret = [m_Owner.m_CacheManager isExist:[self getDevicePath:name]];
		
		if( ret == NO )
			ret = [m_Owner isExistInResource:[self getFileName:name]];
	}
	return ret;
}


- (NSData*)load: (NSString*) name
{
	NSData* data = nil;
	BOOL ret = NO;
	if( name )
	{
		data = [m_Owner.m_CacheManager load:[self getDevicePath:name]];
		
		if( !data )
			data = [m_Owner loadFromResource:name];
		
		if( data )
			ret = [m_XmlParser parseXml:data funcNo:eXmlParseMenuFood];
	}
	return (ret == YES) ? data : nil;
}


- (BOOL)save:(NSData*)data filename:(NSString*)filename
{
	NSData* _data = [data convertXml];
	BOOL ret = NO;
	if( [m_XmlParser parseXml:data funcNo:eXmlParseMenuFood])
		ret = [m_Owner.m_CacheManager save:_data path:[self getDevicePath:filename]];
	return ret;
}


- (void)remove: (NSString*) name
{
	if( name )
		[m_Owner.m_CacheManager remove:[self getDevicePath:name]];
}


- (DLResponse)requestDownload:(id)delegate name:(NSString*)name priority:(int)priority;
{
	NSString* url_path = [self getFileName: name];		
	return [m_Owner.m_Downloader requestDownload:url_path saveName:name delegate:delegate priority:priority];
}


- (BOOL)requestFileXML 
{
	BOOL ret = NO;
    NSBundle* bundle = [NSBundle mainBundle];
	NSString* file_path = [bundle pathForResource: @"Menu1" ofType:@"xml"];   	
	NSData* data = [[NSData dataWithContentsOfFile:file_path] retain];		
	if (data) {		
		[self save:data filename:MENU_FOOD];        
	}
	[data release];
	return ret;
}

@end
